package com.adb.receivecontent.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.adb.receivecontent.R;
import com.adb.receivecontent.bean.LinkMagnetBean;
import com.adb.receivecontent.uc.IMenuClickListener;
import com.adb.receivecontent.uc.LinkMagnetAdapter;
import com.adb.receivecontent.utility.Const;
import com.adb.receivecontent.utility.Pref_Link_Magnet;

public class LinkedCreatedActivity extends Activity implements IMenuClickListener {
	
	private final String TAG = LinkedCreatedActivity.class.getCanonicalName();
	private Button btn_new_link_creating;
	private ListView mlistview_created_linked;
	private LinkMagnetAdapter mLinkMagnetApter;
	private Map<String,String> mLinkKeySets;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == Const.REQUEST_NEW_LINK_CREATE_CODE){
			if(resultCode == RESULT_OK){
				if(mLinkMagnetApter != null)
				updateAdapter();
			}
		}else if(requestCode == Const.REQUEST_EDIT_LINKED_CODE){
			
			if(resultCode == RESULT_OK){
				updateAdapter();
			}
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG,"[ onCreate ] ");
		setContentView(R.layout.activity_linked_created);
		
		mlistview_created_linked = (ListView)findViewById(R.id.listview_created_linked);
		mLinkMagnetApter = new LinkMagnetAdapter(LinkedCreatedActivity.this);
		mlistview_created_linked.setAdapter(mLinkMagnetApter);
		mlistview_created_linked.setEmptyView(findViewById(R.id.empty));
		updateAdapter();
		btn_new_link_creating = (Button)findViewById(R.id.btn_new_link_creating);
		btn_new_link_creating.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent mNewLinkIntent = new Intent(LinkedCreatedActivity.this,NewLinkCreateActivity.class);
				mNewLinkIntent.putExtra(Const.INTENT_KEY_MODE,Const.INTENT_MODE_VALUE_NEW);
				startActivityForResult(mNewLinkIntent,Const.REQUEST_NEW_LINK_CREATE_CODE);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"[ onStart ] ");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG,"[ onStop ] ");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG,"[ onPause ] ");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG,"[ onResume ] ");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"[ onDestroy ] ");
	}

	@Override
	public void onLinkMenuClick(int id,final Object location) {
		
		if (id == R.id.imageview_edit_link) {
			
//			Toast.makeText(LinkedCreatedActivity.this, "Edit click ",Toast.LENGTH_LONG).show();
			
			Intent mEditIntent = new Intent(LinkedCreatedActivity.this,NewLinkCreateActivity.class);
			mEditIntent.putExtra(Const.INTENT_KEY_MODE,Const.INTENT_MODE_VALUE_EDIT);
			mEditIntent.putExtra(Const.INTENT_KEY_LOCATION,String.valueOf(location.toString()));
			startActivityForResult(mEditIntent,Const.REQUEST_EDIT_LINKED_CODE);
			
		} else if (id == R.id.imageview_delete_link) {
			
			Pref_Link_Magnet.removeKeyID(LinkMagnetAdapter.mLinkListLinkMagnet.get(Integer.parseInt(location.toString())).getStrLinkID());
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					mLinkMagnetApter.removeAddedLinkedItems(Integer.parseInt(location.toString()));
					
				}
			}, 100);
			
		}
		
	}
	
	public void updateAdapter(){
		
		new AsyncTask<Void,Void,Integer>(){
			
			ArrayList<LinkMagnetBean> mWholeLinklist = null;
			
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
			}
			
			@Override
			protected Integer doInBackground(Void... params) {
				int result = 0;
				
				if(mLinkMagnetApter !=null){
					
					mWholeLinklist = new ArrayList<LinkMagnetBean>();
					LinkMagnetBean mNewRecord;
					mLinkKeySets = Pref_Link_Magnet.getAllKeySets();
					if(mLinkKeySets != null && mLinkKeySets.size() > 0){
						
						Iterator<Map.Entry<String,String>> linkEntries = mLinkKeySets.entrySet().iterator();
						while(linkEntries.hasNext()){
							
							Map.Entry<String,String> mLinkEntry = linkEntries.next();
							String keyID = mLinkEntry.getKey(); 
							String completeEntry = mLinkEntry.getValue();
							
							Log.d(TAG,"KEYID :" + keyID);
							Log.d(TAG,"VALUE :" + completeEntry);
							
							mNewRecord = new LinkMagnetBean();
							mNewRecord.setStrLinkID(keyID); // Key ID
							mNewRecord.extractWholeString(completeEntry);
							mWholeLinklist.add(mNewRecord);
						}
						
					}else{
						
						result = -1;
					}
					
				}else{
					
					result = -1;
				}
				
				return result;
			}
			
			@Override
			protected void onPostExecute(Integer result) {
				super.onPostExecute(result);
				if(result == 0){
					
					mLinkMagnetApter.addLinksItems(mWholeLinklist);
				}
			}
			
		}.execute();
		
	}
	
}
